import win32gui
import win32con
import win32api


def find_window_handle(window_title):
    # 回调函数，用于EnumWindows时处理每个窗口
    def callback(hwnd, extra):
        window_text = win32gui.GetWindowText(hwnd)
        if window_text and window_title in window_text:
            extra.append(hwnd)
        return True

    handles = []
    win32gui.EnumWindows(callback, handles)
    return handles


def get_title_bar_size(hwnd):
    window_rect = win32gui.GetWindowRect(hwnd)
    client_rect = win32gui.GetClientRect(hwnd)
    title_bar_width = (window_rect[2] - window_rect[0]) - (client_rect[2] - client_rect[0])
    title_bar_height = (window_rect[3] - window_rect[1]) - (client_rect[3] - client_rect[1])
    return window_rect, client_rect, title_bar_width, title_bar_height


# 获取整个屏幕的大小
screen_width = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
screen_height = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)


def get_hwnd_by_enum(title='原神'):
    handles = find_window_handle(title)

    hwnd = 0
    for _hwnd in handles:
        class_name = win32gui.GetClassName(_hwnd)
        if class_name == 'UnityWndClass':
            hwnd = _hwnd
            break
    return hwnd


def set_window_size(hwnd, width=1920, height=1080):
    win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, width, height, win32con.SWP_NOMOVE)


def move_wind_to_left_top(hwnd, client_top_left=False):
    if client_top_left:
        window_rect, client_rect, title_bar_width, title_bar_height = get_title_bar_size(hwnd)
        win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, -title_bar_width, -title_bar_height, 0, 0,
                              win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE)
    else:
        win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE)


def bring_window_to_top(hwnd):
    win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
    win32gui.SetForegroundWindow(hwnd)


def set_genshin_window_position(client_top_left=False, sticky=True):
    hwnd = get_hwnd_by_enum()
    if hwnd == 0:
        return 0

    move_wind_to_left_top(hwnd, client_top_left=client_top_left)
    if sticky:
        bring_window_to_top(hwnd)
    return hwnd


if __name__ == '__main__':
    hwnd = get_hwnd_by_enum('原神')
    assert hwnd, 'hwnd为空?'
    hwnd
    # 设置窗口位置到左上角
    # win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, screen_width // 2, screen_height // 2, win32con.SWP_SHOWWINDOW)
    # 设置窗口位置到左上角，不改变大小
    win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE)

    window_rect, client_rect, title_bar_width, title_bar_height = get_title_bar_size(hwnd)

    # 获取整个窗口的位置信息
    # window_rect = win32gui.GetWindowRect(hwnd)
    window_h = window_rect[2] - window_rect[0]
    window_w = window_rect[3] - window_rect[1]
    window_h - 1280, window_w - 720
    title_bar_width, title_bar_height


    content_window_l_u = window_rect[1] + title_bar_height
    content_window_rect = (window_rect[0], content_window_l_u, window_rect[2], window_rect[-1])
    content_window_rect
    content_window_height = window_rect[-1] - content_window_l_u

    # if handles:
    #     print(f"找到窗口标题包含'{window_title_to_find}'的句柄：{handles}")
    # else:
    #     print(f"未找到窗口标题包含'{window_title_to_find}'的句柄")
